<template>
  <div class='com-container'>
    <div class="title" :style="comStyle">
      <span>{{ '▎ ' + title }}</span>
      <span class="iconfont title-icon" @click="showChoice = !showChoice" :style="comStyle">&#xe6eb;</span>
      <div class="select-con" v-if="showChoice" :style="marginStyle">
        <div class="select-item" v-for="item in selectTypes" :key="item.key" @click="handleSelect(item.key)">
          {{ item.text }}
        </div>
      </div>
    </div>
    <div class='com-chart' ref='dv'></div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      chartInstance: '',
      allData: [],
      dataType: 'map',
      showChoice: false,
      titleFontSize: 0
    }
  },
  computed: {
    selectTypes () {
      if (!this.allData || !this.allData.type) {
        return []
      } else {
        return this.allData.type.filter(item => {
          return item.key !== this.dataType
        })
      }
    },
    title () {
      if (!this.allData) {
        return ''
      } else {
        return this.allData[this.dataType].title
      }
    },
    comStyle () {
      return {
        fontSize: this.titleFontSize + 'px'
      }
    },
    marginStyle () {
      return {
        marginLeft: this.titleFontSize + 'px'
      }
    }
  },
  mounted () {
    this.getData()
    this.initCharts()
    window.addEventListener('resize', this.screenAdapter)
    this.screenAdapter()
  },
  methods: {
    screenAdapter () {
      this.titleFontSize = this.$refs.dv.offsetWidth / 100 * 3.6
      this.chartInstance.resize()
      const adapterOption = {
        legend: {
          itemWidth: this.titleFontSize / 1.5,
          itemHeight: this.titleFontSize / 1.5,
          itemGap: this.titleFontSize / 1.5,
          textStyle: {
            fontSize: this.titleFontSize / 2
          }
        }
      }
      this.chartInstance.setOption(adapterOption)
      this.chartInstance.resize()
    },
    initCharts () {
      this.chartInstance = this.$echarts.init(this.$refs.dv, 'chalk')
      const option = {
        xAxis: {
          type: 'category',
          boundaryGap: false
        },
        yAxis: {
          type: 'value'
        },
        grid: {
          top: '35%',
          left: '3%',
          right: '4%',
          bottom: '1%',
          containLabel: true
        },
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          top: '15%',
          icon: 'circle',
          left: 20
        }
      }
      this.chartInstance.setOption(option)
    },
    handleSelect (key) {
      this.dataType = key
      this.updataChart()
      this.showChoice = false
    },
    async getData () {
      const {
        data: res
      } = await this.$http.get('./data/trend.json')
      this.allData = res
      this.updataChart()
    },
    updataChart () {
      const colorArr1 = [
        'rgba(11, 168, 44, 0.5)',
        'rgba(44, 110, 255, 0.5)',
        'rgba(22, 242, 217, 0.5)',
        'rgba(254, 33, 30, 0.5)',
        'rgba(250, 105, 0, 0.5)'
      ]
      const colorArr2 = [
        'rgba(11, 168, 44, 0)',
        'rgba(44, 110, 255, 0)',
        'rgba(22, 242, 217, 0)',
        'rgba(254, 33, 30, 0)',
        'rgba(250, 105, 0, 0)'
      ]
      const monthArr = this.allData.common.month
      const valueArr = this.allData.[this.dataType].data
      const seriesArr = valueArr.map((item, index) => {
        return {
          type: 'line',
          name: item.name,
          data: item.data,
          stack: this.dataType,
          areaStyle: {
            color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1,
              [{
                offset: 0,
                color: colorArr1[index]
              },
              {
                offset: 1,
                color: colorArr2[index]
              }
              ])
          }
        }
      })
      const dataOption = {
        xAxis: {
          data: monthArr
        },
        legend: {
          data: valueArr
        },
        series: seriesArr
      }
      this.chartInstance.setOption(dataOption)
    }
  }
}
</script>

<style scoped="scoped" lang="less">
.com-container {
  .title {
    position: absolute;
    left: 20px;
    top: 20px;
    z-index: 1;
    color: white;

    .select-con {
      background-color: #222733;
    }

    .select-item {
      cursor: pointer;
    }

    .title-icon {
      margin-left: 10px;
      cursor: pointer;
    }

    .select-item {
      cursor: pointer;
    }
  }
}
</style>
